/**
 * 
 */
package com.mobile2you.nci.android;

import java.util.ArrayList;
import java.util.List;

import org.w3c.dom.Document;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
import android.os.Message;
import android.widget.TextView;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.MyLocationOverlay;
import com.google.android.maps.Overlay;
import com.google.android.maps.OverlayItem;
import com.mobile2you.nci.android.domain.Centres;
import com.mobile2you.nci.android.utils.MarkerTicket;
import com.mobile2you.nci.android.utils.Utils;

/**
 * @author JBromo
 * 
 */
public class MenuCentreMapDetail extends MapActivity implements Callback {

	private MapView mapView;
	private TextView titleCentres;
	private Centres centres;

	public void addMarkers(Centres eventModel) {
		mapView.setBuiltInZoomControls(true);
		mapView.displayZoomControls(true);
		MapController mapController = mapView.getController();
		List<Overlay> mapOverlays = mapView.getOverlays();
		Drawable agencyIconDrawable = this.getResources().getDrawable(
				R.drawable.position);
		GeoPoint point = new GeoPoint((int) (eventModel.getLatitude() * 1E6),
				(int) (eventModel.getLongitude() * 1E6));
		OverlayItem overlayitem = new OverlayItem(point, "", "");
		MarkerTicket iconmarker = new MarkerTicket(agencyIconDrawable, this,
				mapView, null);
		iconmarker.addOverlay(overlayitem, centres);
		mapOverlays.add(iconmarker);
		mapController.setCenter(point);
		mapController.setZoom(15);
		MyLocationOverlay myLocationOverlay = new MyLocationOverlay(this,
				mapView);
		myLocationOverlay.enableMyLocation();
		GeoPoint geoPoint = myLocationOverlay.getMyLocation();
		mapOverlays.add(myLocationOverlay);
		List<Centres> list = new ArrayList<Centres>();
		list.add(eventModel);
		Utils.zoomInBounds(mapView, list);

	}

	@Override
	public boolean handleMessage(Message msg) {
		if (msg.arg1 == 1) {
			Document document = (Document) msg.obj;
			Utils.drawItineraire(document, mapView);
		}

		return false;
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.menu_centre_map_detail);
		titleCentres = (TextView) findViewById(R.id.label_title_centre);
		mapView = (MapView) findViewById(R.id.map_centre);
		centres = Utils.centreSelected;
		titleCentres.setText(centres.getTitle());
		addMarkers(centres);
		if (Utils.userLocation != null) {
			Utils.drawItineraireThread(centres, Utils.userLocation,
					new Handler(this));
		}

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see android.app.Activity#onPause()
	 */
	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		finish();
	}
}
